//
//  TopicDiscussListSondiscusslist_tv_cell.m
//  ainkjj
//
//  Created by MAX on 2020/5/9.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "TopicDiscussListSondiscusslist_tv_cell.h"
#import "Tiezi_pinglun_vc.h"
#import "Tiezi_pinglun_erji_vc.h"

static UIEdgeInsets const kInsets_dis = {15, 64, 15, 16};
static CGSize const kAvatarSize = {25, 25};
static CGFloat const  kAvatarMarginRight = 10;
static CGFloat const  kAvatarMarginBottom = 12;
static CGFloat const  kContentMarginBotom = 12;




@interface TopicDiscussListSondiscusslist_tv_cell()


@end

@implementation TopicDiscussListSondiscusslist_tv_cell

- (void)didInitializeWithStyle:(UITableViewCellStyle)style {
    [super didInitializeWithStyle:style];
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
//    self.backgroundColor = UIColor.redColor;
    //    self.contentView.backgroundColor = UIColor.groupTableViewBackgroundColor;
    //    _bgV = [UIView new];
    //    [self.contentView addSubview:_bgV];
    //    _bgV.backgroundColor = UIColor.whiteColor;
    //    _bgV.layer.masksToBounds = YES;
    //    _bgV.layer.cornerRadius = 5;
    
    _avatarImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.avatarImageView];
    _avatarImageView.layer.masksToBounds = YES;
    _avatarImageView.layer.cornerRadius = kAvatarSize.height * 0.5;
    _nameLabel = [[UILabel alloc] qmui_initWithFont:UIFontBoldMake(16) textColor:rgb(17,17,17)];
    [self.contentView addSubview:self.nameLabel];
    
    
    _timeLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(15) textColor:UIColor.qd_descriptionTextColor];
    [self.contentView addSubview:self.timeLabel];
    
    _contentLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(17) textColor:UIColor.qd_mainTextColor];
    self.contentLabel.numberOfLines = 0;
    [self.contentView addSubview:self.contentLabel];
    
    
    _btn1 = [QMUIButton new];
    [self.contentView addSubview:_btn1];
    [_btn1 addTarget:self action:@selector(dianzan_action:) forControlEvents:UIControlEventTouchUpInside];
    _btn2 = [QMUIButton new];
    [_btn2 addTarget:self action:@selector(erji_pinglun:) forControlEvents:UIControlEventTouchUpInside];

    [self.contentView addSubview:_btn2];
    [_btn1 setTitleColor:rgb(244,110,23) forState:UIControlStateSelected];
    [_btn1 setTitleColor:rgb(153,153,153) forState:UIControlStateNormal];
    [_btn2 setTitleColor:rgb(153,153,153) forState:UIControlStateNormal];
    
    self.btnArr = @[_btn1, _btn2];
    NSArray *imgs = @[@"praise_icon", @"comment_icon"];
    for ( NSUInteger i = 0; i < self.btnArr.count ; i++) {
        QMUIButton *b = self.btnArr[i];
        //        b.tintColorAdjustsTitleAndImage = UIColor.qd_tintColor;
        b.imagePosition = QMUIButtonImagePositionLeft;
        b.spacingBetweenImageAndTitle = 8;
        [b setImage:UIImageMake(imgs[i]) forState:UIControlStateNormal];
        b.titleLabel.font = UIFontMake(16);
        //        b.qmui_borderPosition = QMUIViewBorderPositionTop | QMUIViewBorderPositionBottom;
        if (i == 0) {
            [b setImage:UIImageMake(@"praise_selected_icon") forState:UIControlStateSelected];
        }
    }
    
//    self.contentView.backgroundColor = UIColor.blueColor;

}

- (CGSize)sizeThatFits:(CGSize)size {
    

    CGSize resultSize = CGSizeMake(size.width, 0);
    CGFloat contentLabelWidth = ScreenWidth - kInsets_dis.left - kAvatarSize.width - kAvatarMarginRight - kInsets_dis.right;

    CGFloat resultHeight = UIEdgeInsetsGetVerticalValue(kInsets_dis) + CGRectGetHeight(self.avatarImageView.bounds) + kAvatarMarginBottom;
    
    if (self.contentLabel.text.length > 0) {
        CGSize contentSize = [self.contentLabel sizeThatFits:CGSizeMake(contentLabelWidth, CGFLOAT_MAX) ];
//        DLog(@"---------------实际 子评论contentLb 高度%f", contentSize.height);

        resultHeight += (contentSize.height + kContentMarginBotom);

    }
    
    
    // 按钮布局依据
    resultHeight += 40;
    
    resultSize.height = resultHeight;
    //    DLog(@"%@ 的 cell 的 sizeThatFits: 被调用（说明这个 cell 的高度重新计算了一遍）", self.nameLabel.text);
//    DLog(@"---------------实际cell 高度%f", resultHeight);
    
    return resultSize;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    
    CGFloat contentLabelWidth = ScreenWidth - kInsets_dis.left - kAvatarSize.width - kAvatarMarginRight - kInsets_dis.right;
//    DLog(@"实际 子评论 contentW:%f", contentLabelWidth);
    self.avatarImageView.frame = CGRectMake(kInsets_dis.left, kInsets_dis.top, kAvatarSize.width, kAvatarSize.height);
    if (self.nameLabel.text.length > 0) {
        CGFloat nameLabelWidth = contentLabelWidth;
        //        nameLabelWidth = contentLabelWidth - CGRectGetWidth(self.avatarImageView.bounds) - kAvatarMarginRight - 44;
        
        CGSize nameSize = [self.nameLabel sizeThatFits:CGSizeMake(nameLabelWidth, CGFLOAT_MAX)];
        self.nameLabel.frame = CGRectFlatMake(CGRectGetMaxX(self.avatarImageView.frame) + kAvatarMarginRight, CGRectGetMinY(self.avatarImageView.frame), nameLabelWidth, nameSize.height);
    }
    
    
    if (self.contentLabel.text.length > 0) {
        CGSize contentSize = [self.contentLabel sizeThatFits:CGSizeMake(contentLabelWidth, CGFLOAT_MAX)];
        self.contentLabel.frame = CGRectFlatMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.nameLabel.frame) + kAvatarMarginBottom, contentLabelWidth, contentSize.height);
    }
    
    if (self.timeLabel.text.length > 0) {
        CGSize timeSize = [self.timeLabel sizeThatFits:CGSizeMake(contentLabelWidth, CGFLOAT_MAX)];
        self.timeLabel.frame = CGRectFlatMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.contentLabel.frame) + kContentMarginBotom, timeSize.width, timeSize.height);
    }
    
    // 按钮布局依据
    CGFloat btnY = CGRectGetMaxY(self.contentLabel.frame);
    CGFloat btnW = 54;
    for (NSInteger i = 0; i < self.btnArr.count; i++) {
        QMUIButton *b = self.btnArr[i];
        b.frame = CGRectMake( ScreenWidth - (kInsets_dis.right + (self.btnArr.count - i) * btnW), btnY, btnW, 40);
    }
    
    
}
- (void)setModel:(TopicDiscussListSondiscusslist *)model{
       _model = model;
    
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:model.discussAvatarUrl] placeholderImage:UIImageMake(@"mine_head_icon")];
    self.contentLabel.text = model.content;
    self.timeLabel.text = model.discussTime;
    
    
    
    // 按钮赋值
    [self.btn1 setTitle:@(model.praiseNum).stringValue forState:UIControlStateNormal];
    
    if (model.praiseStatus == 1) {
        self.btn1.selected = NO;
    }else{
        self.btn1.selected = YES;
    }
    
    
    [self.btn2 setTitle:@(model.discussNum).stringValue forState:UIControlStateNormal];
    
    TopicDiscussListSondiscusslist *m = (TopicDiscussListSondiscusslist *)model;
    self.nameLabel.text = [NSString stringWithFormat:@"%@ ▷ %@", m.discussNikeName, m.byDiscussNikeName];//▶→›＞→
    
}
- (void)erji_pinglun:(QMUIButton *)sender{
    Tiezi_pinglun_vc *vc = (Tiezi_pinglun_vc *)[Anktool viewControllerForView:self];
    vc.huifuId = self.model.ID;
    vc.beihuifuren_lb.text = [NSString stringWithFormat:@"评论给%@",self.model.discussNikeName];
    [vc.textView becomeFirstResponder];
}
- (void)dianzan_action:(QMUIButton *)sender{
    sender.userInteractionEnabled = NO;
    
    NSString *url = sender.isSelected == YES ? @"java/app/topic/repealTopicLike" : @"java/app/topic/topicLike";
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:app_code forKey:@"appCode"];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"token"];
    [dict setObject:@(self.model.topicId).stringValue forKey:@"topicId"];
    [dict setObject:@(self.model.ID).stringValue forKey:@"discussId"];

    
    //sender.userInteractionEnabled = NO;
    
    
    [PPHTTPRequest req:url parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            
            
            if (sender.selected) {
                self.model.praiseNum -=1;
            }else{
                self.model.praiseNum +=1;
            }
            sender.selected =  !sender.isSelected ;
            
            [self.btn1 setTitle:@(self.model.praiseNum).stringValue forState:UIControlStateNormal];
        }
        else{
            
            
            [QMUITips showError:response[@"error_msg"]];
            
        }
        sender.userInteractionEnabled = YES;
    } failure:^(NSError *error) {
        [QMUITips showError:[error localizedDescription] ];
        sender.userInteractionEnabled = YES;
    }];
}


@end
